#include <bits/stdc++.h>

using namespace std;

priority_queue<int, vector<int>, greater<int> > pq;

int main(){
    int n;
    long long tot = 0; //当前的总长
    long long ans = 0;
    cin >> n;
    for(int i = 1; i <= n; i++){
        int tmp;
        cin >> tmp;
        tot += tmp;
        pq.push(tmp);
    }
    while(pq.size() > 1){
        int l = pq.top();   pq.pop();
        int ll = pq.top();  pq.pop();
        ans += l + ll;
        pq.push(l + ll);
    }
    cout << ans;
    return 0;
}